<? if ($c = stack('name')) {

//	$d = component_data($c); 
	$s = component_settings($c);

	
startScript('jquery, jquery.form'); ?><script type="text/javascript">
jQuery(function($) {	
	$("#<?="cpn-$c-form"?>").ajaxForm({ dataType: "json", 
		success: function(data) { 
			var diags = data.diags ? data.diags.replace(/&lt;/g, "<").replace(/&gt;/g, ">") : "";
			if (diags) $("#<?="cpn-$c-diags"?>").html(diags).show().css("opacity", 1).fadeTo(3000, 0).slideUp(500);	}
	}); 
});
</script><? endScript();

	?><div id="<?="cpn-$c"?>" class="cpn-form-ajax"><?

	ob_start();
	
	?><div id="<?="cpn-$c-diags"?>" class="diags"><p><?=implode("</p><p>", $DIAGS)?></p></div><?
	
	?><form id="<?="cpn-$c-form"?>" action="./" method="post" enctype="multipart/form-data"><?
	
		switch($s['markup']) {
			case 0: ?><table><? break;	// table
			case 1: ?><dl><? break;	// definition list
			case 2: ?><ul><? break;	// placeholders
		}
		
		
		
		if (sizeof($d = component_data($c))) {

			foreach($d as $id => $a) {
			
				switch($a['kind']) {
					
					case 1: //text field
					
						switch($s['markup']) {

							case 0: 	// table
							
								?><tr>
									<td class="b-field-name">
									  <label for="field<?=$id?>"><?=$a['title']?><?	if ($a['required']) { ?><strong>*</strong><? } ?></label>
									</td>
									<td class="b-field-value"><input type="text" name="field<?=$id?>" id="field<?=$id?>" class="form-input" value="<?=h($_POST["field$id"])?>" /></td>
								</tr><?

								break;
								
							case 1:	// definition list

								?><dt><label for="field<?=$id?>"><?=$a['title']?><? if ($a['required']) { ?><strong>*</strong><? } ?></label></dt>
									<dd><input type="text" name="field<?=$id?>" id="field<?=$id?>" class="form-input" value="<?=h($_POST["field$id"])?>" /></dd><?

								break;
								
							case 2:	// placeholders

								?><li><input type="text" name="field<?=$id?>" id="field<?=$id?>" class="form-input" value="<?=h($_POST["field$id"])?>" placeholder="<?=h($a['title'])?>" /></li><?
								
								startScript('jquery.enablePlaceholder'); ?><script type="text/javascript">jQuery(function($){
									$("#field<?=$id?>").enablePlaceholder();
								});</script><? endScript();

								break;
						}

						break;

					case 2: //textarea field

						switch($s['markup']) {

							case 0: 	// table
							
								?><tr>
									<td class="b-field-name">
									  <label for="field<?=$id?>"><?=$a['title']?><?	if ($a['required']) { ?><strong>*</strong><? } ?></label>
									</td>
									<td class="b-field-value"><textarea name="field<?=$id?>" id="field<?=$id?>" class="form-input" rows="10"><?=$_POST["field$id"]?></textarea></td>
								</tr><?

								break;
								
							case 1:	// definition list

								?><dt><label for="field<?=$id?>"><?=$a['title']?><? if ($a['required']) { ?><strong>*</strong><? } ?></label></dt>
									<dd><textarea name="field<?=$id?>" id="field<?=$id?>" class="form-input" rows="10"><?=$_POST["field$id"]?></textarea></dd><?

								break;
								
							case 2:	// placeholders

								?><li><textarea name="field<?=$id?>" id="field<?=$id?>" class="form-input" rows="10" placeholder="<?=h($a['title'])?>"><?=$_POST["field$id"]?></textarea></li><?

								startScript('jquery.enablePlaceholder'); ?><script type="text/javascript">jQuery(function($){
									$("#field<?=$id?>").enablePlaceholder();
								});</script><? endScript();

								break;
						}

						break;

					case 3: //checkboxes field

						switch($s['markup']) {

							case 0: 	// table
							
								?><tr>
									<td class="b-field-name">
									  <label><?=$a['title']?><? if ($a['required']) { ?><strong>*</strong><? } ?></label>
									</td>
									<td class="b-field-value"><?
										if ($a['listbox']) {
											?><div><select name="field<?=$id?><?=$a['multiple'] ? '[]' : ''?>"<?=$a['multiple'] ? ' multiple="multiple"' : ''?>><?
												foreach($a['options'] as $k => $v) {
													?><option value="<?=$k?>"><?=$v?></option><?
												}
											?></select></div><?
										} else {
											if ($a['multiple']) {
												foreach($a['options'] as $k => $v) {
													?><label><input type="checkbox" name="field<?=$id?>[]" value="<?=$k?>" /><span><?=$v?></span></label><?
												}
											} else {
												foreach($a['options'] as $k => $v) {
													?><label><input type="radio" name="field<?=$id?>" value="<?=$k?>" /><span><?=$v?></span></label><?
												}
											}
										}
									?></td>
								</tr><?

								break;
								
							case 1:	// definition list

								?><dt><label><?=$a['title']?><? if ($a['required']) { ?><strong>*</strong><? } ?></label></dt>
									<dd><?
										if ($a['listbox']) {
											?><div><select name="field<?=$id?><?=$a['multiple'] ? '[]' : ''?>"<?=$a['multiple'] ? ' multiple="multiple"' : ''?>><?
												foreach($a['options'] as $k => $v) {
													?><option value="<?=$k?>"><?=$v?></option><?
												}
											?></select></div><?
										} else {
											if ($a['multiple']) {
												foreach($a['options'] as $k => $v) {
													?><label><input type="checkbox" name="field<?=$id?>[]" value="<?=$k?>" /><span><?=$v?></span></label><?
												}
											} else {
												foreach($a['options'] as $k => $v) {
													?><label><input type="radio" name="field<?=$id?>" value="<?=$k?>" /><span><?=$v?></span></label><?
												}
											}
										}
									?></dd><?

								break;
								
							case 2:	// placeholders

								?><li><label><?=$a['title']?><? if ($a['required']) { ?><strong>*</strong><? } ?></label><?
										if ($a['listbox']) {
											?><div><select name="field<?=$id?><?=$a['multiple'] ? '[]' : ''?>"<?=$a['multiple'] ? ' multiple="multiple"' : ''?>><?
												foreach($a['options'] as $k => $v) {
													?><option value="<?=$k?>"><?=$v?></option><?
												}
											?></select></div><?
										} else {
											if ($a['multiple']) {
												foreach($a['options'] as $k => $v) {
													?><label><input type="checkbox" name="field<?=$id?>[]" value="<?=$k?>" /><span><?=$v?></span></label><?
												}
											} else {
												foreach($a['options'] as $k => $v) {
													?><label><input type="radio" name="field<?=$id?>" value="<?=$k?>" /><span><?=$v?></span></label><?
												}
											}
										}
									?></li><?

								break;
						}
						
						break;
						
					case 4: //file upload

						switch($s['markup']) {

							case 0: 	// table
							
								?><tr>
									<td class="b-field-name">
									  <label for="field<?=$id?>"><?=$a['title']?><? if ($a['required']) { ?><strong>*</strong><? } ?></label>
									</td>
									<td class="b-field-value"><input type="file" name="field<?=$id?>" id="field<?=$id?>" class="form-input" /></td>
								</tr><?

								break;
								
							case 1:	// definition list

								?><dt><label for="field<?=$id?>"><?=$a['title']?><? if ($a['required']) { ?><strong>*</strong><? } ?></label></dt>
									<dd><input type="file" name="field<?=$id?>" id="field<?=$id?>" class="form-input" /></dd><?

								break;
								
							case 2:	// placeholders

								?><li><label for="field<?=$id?>"><?=$a['title']?><? if ($a['required']) { ?><strong>*</strong><? } ?></label><input type="file" name="field<?=$id?>" id="field<?=$id?>" class="form-input" /></li><?

								break;
						}

						break;
						
					case 5: //captcha

						switch($s['markup']) {

							case 0: 	// table
							
								?><tr>
									<td class="b-field-name">
										<div id="<?="cpn-$c-cap"?>"></div>
									</td>
									<td class="b-field-value"><input type="text" name="field<?=$id?>" id="field<?=$id?>" class="form-input" value="" /></td>
								</tr><?

								break;
								
							case 1:	// definition list

								?><dt><div id="<?="cpn-$c-cap"?>"></div></dt>
									<dd><input type="text" name="field<?=$id?>" id="field<?=$id?>" class="form-input" value="" /></dd><?

								break;
								
							case 2:	// placeholders

								?><li><div id="<?="cpn-$c-cap"?>"></div><input type="text" name="field<?=$id?>" id="field<?=$id?>" class="form-input" value="" /></li><?

								break;
						}
						
						startScript(); ?><script type="text/javascript">
							jQuery(function($) {
								var div = $("#<?="cpn-$c-cap"?>:visible");
								if (div.length) {
									var r = Date.getTime();
									div.html('<img src="/captcha/?<?=$id?>:' + r + '" /><input type="hidden" name="cap<?=$id?>" value="' + r + '" />');
								else
									$("#<?="cpn-$c-cap"?>").bind("getImage", function() {
										var r = Date.getTime();
										$(this).html('<img src="/captcha/?<?=$id?>:' + r + '" /><input type="hidden" name="cap<?=$id?>" value="' + r + '" />');
									});
							});
						</script><? endScript();
						
						break;
						
				}
			} 
		}

		switch($s['markup']) {
			case 0: ?></table><? break;	// table
			case 1: ?></dl><? break;	// definition list
			case 2: ?></ul><? break;	// placeholders
		}

		?><input type="hidden" name="check" value="<?=h($s['check'])?>" />
		<p><input type="submit" class="submit" name="send_form" value="<?=$s['form_send']?>" /></p>
		</form>
	<?

		$form = ob_get_clean();

		$cap = "<div class='cpn-form-caption'>$s[caption]</div>";
		
		$html = (strpos($cap, '{form}') !== false) ? str_replace('{form}', "</div>$form<div class='cpn-form-footer'>", $cap) : $cap.$form;
		
	?><?=$html?></div><?
	
	unstack();
}